/**
 * Copyright 2012 Fonfon.Fenceer
 * All rights reserved.
 * 
 * @project 
 * @author Fonfon.Fenceer
 * @version 1.0
 * @data 2012-3-14
 */
package com.guidecaptain.java.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;

import com.guidecaptain.java.mapper.GuideMapper;
import com.guidecaptain.java.mapper.UserMapper;
import com.guidecaptain.java.model.pojo.Guide;
import com.guidecaptain.java.model.pojo.User;
import com.guidecaptain.java.model.vo.GuideVO;
import com.guidecaptain.java.service.GuideService;

/**
 * 
 * @author fenceer
 */
@Service("guideService")
public class GuideServiceImpl implements GuideService {
	private Logger LOG = Logger.getLogger(GuideServiceImpl.class);

	@Autowired
	private GuideMapper guideMapper;
	@Autowired
	private UserMapper userMapper;

	/**
	 * @param
	 * @return
	 */
	@Override
	public int insertGudie(GuideVO guideVO) {
		Guide guide = getGuide(guideVO);

		// 如果还不存在用户，则添加用户
		if (guideVO.getUserId() == 0) {
			User user = new User();
			user.setUsername(guideVO.getUsername());
			user.setName(guideVO.getName());
			user.setRole(3);// 导游角色为3
			Md5PasswordEncoder encoder = new Md5PasswordEncoder();
			user.setPsw(encoder.encodePassword("123456", null));
			this.userMapper.insertUser(user);
			// 将导游ID设置为用户ID
			guide.setGuideId(user.getUserId());
		}else {
			guide.setGuideId(guideVO.getUserId());
		}

		this.guideMapper.insertGuide(guide);
		return guide.getGuideId();
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public int deleteGudie() {

		return 0;
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public int updateGudie(GuideVO guideVO) {

		return 0;
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public Guide queryGuideById(int guideId) {
		Guide guide = new Guide();
		guide.setGuideId(guideId);
		List<Guide> guides = guideMapper.queryGuide(guide);
		if (guides.size() > 0) {
			guide = guides.get(0);
		} else {
			//表示导游信息不存在
			guide.setGuideId(0);
		}
		return guide;
	}

	/**
	 * 分页查询领队导游
	 * @param
	 * @return
	 */
	@Override
	public GuideVO queryGuideList(GuideVO guideVO) {
		Guide guide=getGuide(guideVO);
		guideVO.setTotalRows(guideMapper.countGuides(getGuide(guideVO)));
		guide.setStart(guideVO.getStart());
		guide.setTotalPages(guideVO.getTotalPages());
		guideVO.setGuideList(guideMapper.queryGuide(getGuide(guideVO)));
		return guideVO;
	}

	/**
	 * 将UserVO转换成User
	 * 
	 * @param userVO
	 * @return user
	 */
	private Guide getGuide(GuideVO guideVO) {
		Guide guide = new Guide();
		try {
			PropertyUtils.copyProperties(guide, guideVO);
		} catch (Exception e) {
			LOG.error(e);
		}

		return guide;
	}

	/**
	 * @param 
	 * @return  
	 */
	@Override
	public JSONObject getGuideNameList(Guide guide) {
		System.out.println(guide.getPageSize());
		List<Guide> guides=guideMapper.getGuideNameList(guide);
		JSONObject nameJsonp=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(Guide gd:guides){
			Map<String, Object> jsonMap=new HashMap<String, Object>();
			jsonMap.put("guideId", gd.getGuideId());
			jsonMap.put("guideName", gd.getName());
			jsonArray.add(jsonMap);
		}
		
		nameJsonp.put("namelist", jsonArray);
		System.out.println(nameJsonp.toString());
		return nameJsonp;
	}

}
